import java.io.*;
import java.net.*;
import java.util.*;


public class GDMessageHandler extends Thread{
	GroupDispatcher gd;
	ServerSocket UISocket;
	Socket connectionSocket;
	byte[] UIMessage;
	public GDMessageHandler(GroupDispatcher d){
		gd = d;
	}
	public void run() {
		
		//NOTE: UI will communicate with Server for Hall Requests via PORT 1000
		
		/*
		try {
			UISocket = new ServerSocket(1000);
			connectionSocket = UISocket.accept();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		while(true){
           BufferedReader inFromClient;
           try {
        	   inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        	   UIMessage = inFromClient.readLine().getBytes();
           } catch (IOException e) {
        	   e.printStackTrace();
           }
		*/
		int i = 1;
		char[] r = {'U', 'D', 'N', 'B'};//B for bad direction testing
		while(true){
           int floor = (i % 21)+1; //gets floor from message. Make sure between 1-21. 21 for bad floor testing
           char direction = r[i%3]; //gets direction from message. Make sure 'U', 'D', 'N'

           //sends information to elevator chosen to handle request
           try {
        	   gd.determineElevator(floor, direction).send(floor);
           } catch (IOException e) {
        	   e.printStackTrace();
           }
           try {
			Thread.sleep(1000);//sleep 1 second after every request
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		}
	}
}